{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 3)\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "print re.match('www','www.jiangcheng.com').span()\n",
    "print re.match('com','www.jiangcheng.com')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "matchObj.group(): Cats are smarter than dogs\n",
      "matchObj.group(1) Cats\n",
      "matchObj.group(2) smarter\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "line = 'Cats are smarter than dogs'\n",
    "matchObj = re.match(r'(.*) are (.*?) .*',line,re.M|re.I)\n",
    "\n",
    "if matchObj:\n",
    "    print 'matchObj.group():',matchObj.group()\n",
    "    print 'matchObj.group(1)',matchObj.group(1)\n",
    "    print 'matchObj.group(2)',matchObj.group(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 3)\n",
      "(15, 18)\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "print re.search('www','www.jiangcheng.com').span()\n",
    "print re.search('com','www.jiangcheng.com').span()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "matchObj.group(): Cats are smarter than dogs\n",
      "matchObj.group(1) Cats\n",
      "matchObj.group(2) smarter\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "line = 'Cats are smarter than dogs'\n",
    "matchObj = re.search(r'(.*) are (.*?) .*',line,re.M|re.I)\n",
    "\n",
    "if matchObj:\n",
    "    print 'matchObj.group():',matchObj.group()\n",
    "    print 'matchObj.group(1)',matchObj.group(1)\n",
    "    print 'matchObj.group(2)',matchObj.group(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No match\n",
      "match --> matchObj.group() :  dogs\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "line = 'Cats are smarter than dogs'\n",
    "\n",
    "matchObj = re.match(r'dogs',line,re.M|re.I)\n",
    "if matchObj:\n",
    "    print \"match --> matchObj.group() : \",matchObj.group()\n",
    "else:\n",
    "    print \"No match\"\n",
    "    \n",
    "matchObj = re.search(r'dogs',line,re.M|re.I)\n",
    "if matchObj:\n",
    "    print \"match --> matchObj.group() : \",matchObj.group()\n",
    "else:\n",
    "    print \"No match\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "电话号码是 2004-959-559 \n",
      "电话号码是 2004959559\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "phone = \"2004-959-559 # 这是一个国外电话号码\"\n",
    " \n",
    "# 删除字符串中的 Python注释 \n",
    "num = re.sub(r\"#.*$\",\"\",phone)\n",
    "print \"电话号码是\",num\n",
    "\n",
    "# 删除非数字(-)的字符串 \n",
    "# num = re.sub(r'-','',phone)\n",
    "# print \"电话号码是\",num\n",
    "num = re.sub(r'\\D','',phone)\n",
    "print \"电话号码是\",num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A46G8HFD1134\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "def double(matched):\n",
    "    value = int(matched.group('value'))\n",
    "    return str(value * 2)\n",
    "\n",
    "s = 'A23G4HFD567'\n",
    "print re.sub('(?P<value>\\d+)',double,s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "None\n",
      "<_sre.SRE_Match object at 0x0000000005571510>\n",
      "12\n",
      "3\n",
      "5\n",
      "(3, 5)\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "pattern = re.compile(r'\\d+')\n",
    "m = pattern.match(\"one12twothree34four\")\n",
    "print m\n",
    "\n",
    "m = pattern.match(\"one12twothree34four\",2,10)\n",
    "print m\n",
    "\n",
    "m = pattern.match(\"one12twothree34four\",3,10)\n",
    "print m\n",
    "\n",
    "print m.group(0)\n",
    "print m.start(0)\n",
    "print m.end(0)\n",
    "print m.span(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<_sre.SRE_Match object at 0x00000000053C37A0>\n",
      "Hello World\n",
      "(0, 11)\n",
      "Hello\n",
      "(0, 5)\n",
      "World\n",
      "(6, 11)\n",
      "('Hello', 'World')\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "no such group",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0mTraceback (most recent call last)",
      "\u001b[1;32m<ipython-input-35-d094ec4e8301>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgroups\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: no such group"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "pattern = re.compile(r'([a-z]+) ([a-z]+)',re.I)\n",
    "m = pattern.match('Hello World Wide Web')\n",
    "print m\n",
    "\n",
    "print m.group(0)\n",
    "# print m.start(0)\n",
    "# print m.end(0)\n",
    "print m.span(0)\n",
    "\n",
    "print m.group(1)\n",
    "print m.span(1)\n",
    "\n",
    "print m.group(2)\n",
    "print m.span(2)\n",
    "\n",
    "print m.groups()\n",
    "\n",
    "print m.group(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['123', '456']\n",
      "['88', '12']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "pattern = re.compile(r'\\d+')\n",
    "result1 = pattern.findall('runoob 123 google 456')\n",
    "result2 = pattern.findall('run88oob123google456', 0, 10)\n",
    "\n",
    "print result1\n",
    "print result2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "32\n",
      "43\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "it = re.finditer(r'\\d+',\"12a32bc43jf3\")\n",
    "for match in it:\n",
    "    print match.group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['runoob', 'runoob', 'runoob', '']\n",
      "['runoob', ', ', 'runoob', ', ', 'runoob', '.', '']\n",
      "['', 'runoob, runoob, runoob.']\n",
      "['hello world']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "print re.split('\\W+','runoob, runoob, runoob.')\n",
    "print re.split('(\\W+)','runoob, runoob, runoob.')\n",
    "print re.split('\\W+',' runoob, runoob, runoob.',1)\n",
    "\n",
    "print re.split('a*', 'hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'province': '110', 'city': '223', 'born_year': '1990'}\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "s = '1102231990xxxxxxxx'\n",
    "\n",
    "# 是用来 匹配生成字典\n",
    "res = re.search('(?P<province>\\d{3})(?P<city>\\d{3})(?P<born_year>\\d{4})',s)\n",
    "print(res.groupdict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "matchObj.group() :  Cats are smarter than dogs\n",
      "matchObj.group(1) :  Cats\n",
      "matchObj.group(2) :  smarter\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "line = \"Cats are smarter than dogs\"\n",
    "matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)\n",
    "if matchObj:\n",
    "    print \"matchObj.group() : \", matchObj.group()\n",
    "    print \"matchObj.group(1) : \", matchObj.group(1)\n",
    "    print \"matchObj.group(2) : \", matchObj.group(2)\n",
    "else:\n",
    "    print \"No match!!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
